package org.jxc.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.InOrderProductDAO;
import org.jxc.domain.InOrderProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;

@Service
public class InOrderProductService implements IService {

	private InOrderProductDAO inOrderProductDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		inOrderProductDAO = (InOrderProductDAO) DAOFactory.getDAO(
				sessionFactory, InOrderProductDAO.class);
	}

	public List<InOrderProduct> findByInOrderId(String inOrderId)
			throws CRUDException {
		return inOrderProductDAO.find(
				"from InOrderProduct where inOrderId = ?", inOrderId);
	}

	/**
	 * 保存入库明细
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void updateInOrderProduct(InOrderProduct iop) throws CRUDException {
		InOrderProduct old = inOrderProductDAO.get(iop.getId());
		old.setCode(iop.getCode());
		old.setInAmounts(iop.getInAmounts());
		old.setInPrice(iop.getInPrice());
		old.setProduct(iop.getProduct());
		old.setTotalPrice(iop.getInPrice() * iop.getInAmounts());
		old.setArriveAmounts(iop.getArriveAmounts());
		old.setInOrder(iop.getInOrder());
		inOrderProductDAO.update(old);

//		ProductWarehouseStock pws = stockDAO.findUniqueByProperty("inOrderProduct",
//				iop);
//		if (pws == null) {
//			pws = new ProductWarehouseStock();
//		}
//		pws.setAmounts(iop.getInAmounts());
//		pws.setInOrder(iop.getInOrder());
//		pws.setInOrderProduct(iop);
//		pws.setProduct(iop.getProduct());
//		pws.setWarehouse(iop.getWareHouse());
//		stockDAO.saveOrUpdate(pws);
	}

	public InOrderProduct findById(String id) throws CRUDException {
		return inOrderProductDAO.get(id);
	}
}
